@echo off
chcp 65001 >nul
echo ╔══════════════════════════════════════════════════════════╗
echo ║     FBro 后台鼠标自动化工具 - 构建运行脚本             ║
echo ╚══════════════════════════════════════════════════════════╝
echo.

echo [1/4] 检查 NuGet 包...
echo ──────────────────────────────────────────────────────────
if exist "packages.config" (
    echo ✓ packages.config 存在
    nuget restore 2>nul
    if errorlevel 1 (
        echo ✗ NuGet 还原失败！请确保已安装 NuGet
        echo   下载地址: https://www.nuget.org/downloads
        pause
        exit /b 1
    )
    echo ✓ NuGet 包还原完成
) else (
    echo ✗ 未找到 packages.config
    pause
    exit /b 1
)
echo.

echo [2/4] 查找 MSBuild...
echo ──────────────────────────────────────────────────────────
set MSBUILD_PATH=""
for %%i in (
    "C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe"
    "C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\MSBuild.exe"
    "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe"
    "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe"
    "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe"
) do (
    if exist %%i (
        set MSBUILD_PATH=%%i
        goto :found_msbuild
    )
)

:found_msbuild
if %MSBUILD_PATH%=="" (
    echo ✗ 未找到 MSBuild！
    echo   请安装 Visual Studio 2019 或更高版本
    echo   下载地址: https://visualstudio.microsoft.com/
    pause
    exit /b 1
)
echo ✓ 找到 MSBuild: %MSBUILD_PATH%
echo.

echo [3/4] 编译项目...
echo ──────────────────────────────────────────────────────────
%MSBUILD_PATH% "后台鼠标自动化工具.csproj" /p:Configuration=Debug /p:Platform=AnyCPU /v:m
if errorlevel 1 (
    echo ✗ 编译失败！请检查错误信息
    pause
    exit /b 1
)
echo ✓ 编译成功
echo.

echo [4/4] 启动程序...
echo ──────────────────────────────────────────────────────────
if exist "bin\Debug\后台鼠标自动化工具.exe" (
    echo ✓ 找到可执行文件
    echo ✓ 正在启动程序...
    echo.
    start "" "bin\Debug\后台鼠标自动化工具.exe"
    echo.
    echo ╔══════════════════════════════════════════════════════════╗
    echo ║  ✅ 程序已启动！                                         ║
    echo ║                                                          ║
    echo ║  使用说明:                                               ║
    echo ║  1. 等待浏览器加载完成                                   ║
    echo ║  2. 点击"普通拖动"或"贝塞尔曲线拖动"                      ║
    echo ║  3. 观察拖动效果                                         ║
    echo ║  4. 点击"重置验证码"重新测试                             ║
    echo ╚══════════════════════════════════════════════════════════╝
) else (
    echo ✗ 未找到可执行文件
    echo   请检查编译是否成功
    pause
    exit /b 1
)

pause

